package com.yang.controller.SendController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**java 模拟测试多线程测试http请求
 * @author wdh
 *
 */

public class ServiceThreadTest implements Runnable{

    private String url;

    private Map<String,Object> paramStr;

    public ServiceThreadTest(String url, Map<String,Object> paramStr) {
        super();
        this.url = url;
        this.paramStr = paramStr;
    }


    public String getUrl() {
        return url;
    }


    public void setUrl(String url) {
        this.url = url;
    }


    public  Map<String,Object> getParamStr() {
        return paramStr;
    }


    public void setParamStr(Map<String,Object> paramStr) {
        this.paramStr = paramStr;
    }


    @Override
    public void run() {
        // http请求实现方式
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(200000).setConnectTimeout(200000)
                .setSocketTimeout(100000).build();
        CloseableHttpResponse response = null;
        try {
            List<NameValuePair> params = setHttpNameValues(paramStr);
            HttpEntity httpentity=new UrlEncodedFormEntity(params,"utf-8");
            post.setEntity(httpentity);
            post.setConfig(config);
            response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString(entity);
            System.out.println("content:" + content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<NameValuePair> setHttpNameValues(Map<String,Object> paramMap) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (Entry<String, Object> entry:paramMap.entrySet()){
            params.add(new BasicNameValuePair(entry.getKey(),entry.getValue().toString()));
        }

        return params;
    }


    public static void main(String[] args) {
        //运用java工具类中线程池

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        int kernelCount = Runtime.getRuntime().availableProcessors();
        //核心线程数
        executor.setCorePoolSize(20);
        //最大线程数
        executor.setMaxPoolSize(32);
        //额外线程等待时间
        executor.setKeepAliveSeconds(60);
        //队列长度
        executor.setQueueCapacity(50);
        //线程名称
        executor.setThreadNamePrefix("sjhl-asyncExecutor");
        //拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        for (int i =0;i<25;i++) { //开启俩个线程
            String url = "http://localhost:8080/f1/f2";
            Map<String,Object> paramStr = getHttpParamStr();
            executor.execute(new ServiceThreadTest(url,paramStr));
        }
    }

    public  static Map<String,Object> getHttpParamStr() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("apiversion", 1);
        param.put("appversion", "3.6.2");
        return param;
    }
}
